// index.js 首页登录页面
import http from "../../utils/baseApi.js";

Page({
	// 页面初始数据
	data: {
    	phone:"",
    	code:"",
    	sendTime:"发送验证码",
    	sendColor:"#363636",
		snsMsgWait:60,
		strSessionKey:""
	},
	// 输入手机号事件
	inputPhone: function (e) {
    	this.setData({ phone:e.detail.value });
	},
	// 输入验证码事件
	inputCode: function (e) {
    	this.setData({ code:e.detail.value });
	},
	// 获取验证码
	sendCode: function () {
		let that = this;
		// 手机号码为空,提示
		if ( ! this.data.phone) {
			this.toast("请输入手机号"); return;
		}
		// 手机号码验证,正则
		if ( ! (/^1[3|4|5|8][0-9]\d{8}$/.test(this.data.phone))) {
    		this.toast("手机号输入错误"); return;
		}
		
		// 60秒后重新获取验证码
    	let inter = setInterval(function() {
			that.setData({ smsFlag:true, sendColor:"#ccc", sendTime:that.data.snsMsgWait+"s后重发", snsMsgWait:that.data.snsMsgWait-1 });
        	if (that.data.snsMsgWait < 0) {
            	clearInterval(inter);
            	that.setData({ sendColor:"#363636", sendTime:"发送验证码", snsMsgWait:60, smsFlag:false });
        	}
    	}, 1000);
		// 向服务器发送下发短信请求
      	http.post("sale/sms/send",{
        	phone:that.data.phone
      	}).then((ret) => {
        	if (ret.code == 2000) {
				that.toast("验证码已发送"); return;
			}
        	that.toast("验证码发送失败");
    	});
	},
	// 显示提示框toast方法抽取
	toast: function (msg) {
    	wx.showToast({ title:msg, icon:"none", duration:2000, mask:true });
	},

	// 提交信息,登录或注册
	login: function (phone) {
		let that = this;
		// request 登录
    	http.post("sale/login",{
    		phone:phone
    	}).then((ret) => {
    		if (ret.code == 2000) {
				let data = ret.data;
        		wx.setStorageSync("strUserId",data.strUserId);
				wx.showToast({ title:ret.message, icon:"success", duration:2000, mask:true });
				// 通过获取到的	token查询用户的基础信息
				http.post("sale/information",{
					strUserId:data.strUserId
				}).then((res)=>{
					if (res.code == 2000) {
						wx.setStorageSync("userInfo",res.data);
					}
				});

				let timer = setTimeout(()=>{
					clearTimeout(timer);
					wx.redirectTo({ url:"/pages/main/main" });
				},2000);
    		} else {
    			that.toast(ret.msg ? ret.msg : "小伙伴太热情了");
    		}
    	});
	},
	// 获取用户信息事件
	onGetUserInfo: function () {
		let that = this;
		let strCode = "";
		wx.login({
			success:(res)=>{
				if (res.errMsg == "login:ok") {
					strCode = res.code;
					// 以下调试新接口php
					http.post("wechat/login/key",{
						strCode:strCode
					}).then((res)=>{
						if (res.code == 2000) {
							that.setData({ strSessionKey:res.data.key });
						} else {
							wx.showModal({ title:"授权登录", content:res.content, showCancel:false });
						}
					}); 
					// 当前线上版本接口
					/*http.post("sale/login/session",{
						strCode:strCode
					}).then((res)=>{
						if (res.code == 2000) {
							let data = res.data;
							if (data.sessionKey != null && data.sessionKey != "") {
								that.setData({ strSessionKey:data.sessionKey });
							}
						}
					});*/
				} else {
					wx.showModal({ title:"授权登录", content:"一键授权登录失败, 请重试!", showCancel:false });
				}
			}
		});
		wx.getUserProfile({
			desc: "获取用户信息以提供更好的服务",
			success:(res)=>{
				if (res.userInfo) {
					wx.setStorageSync("strUserAvatar", res.userInfo.avatarUrl);
				}
			}
		})
	},
	onGetPhone: function (e) {
		let strEncryptedData = e.detail.encryptedData;
		let strIv = e.detail.iv;
		let strContent = e.detail.errMsg;
		let that = this;

		if (strContent == "getPhoneNumber:ok") {
			http.post("wechat/information/mobile",{
				strEncryptedData:strEncryptedData,
				strIv:strIv,
				strKey:that.data.strSessionKey
			}).then((res)=>{
				if (res.code == 2000) {
					let mobile = res.data.phoneNumber;
					// 判定是否已是登记注册的医生,如是医生,则提示不能继续. 否则可跳转
					http.post("wechat/login/mobile",{
						strMobile:mobile
					}).then((res)=>{
						console.log(res);
						if (res.code == 2000) {
							wx.showToast({ title:"成功登录", icon:"success", mask:true, duration:1500 });
							wx.setStorageSync("cellphone",mobile);
							wx.setStorageSync("strUserId",res.content);
							let timer = setTimeout(()=>{
								clearTimeout(timer);
								wx.redirectTo({ url:"/pages/main/main" });
							},1500);
						} else {
							wx.showModal({ title:"授权登录", content:res.content, showCancel:false });
						}
					});
				}
			});
			/*
			http.post("sale/login/wechat",{ 
				strEncryptedData:strEncryptedData,
				strIv:strIv,
				strSessionKey:that.data.strSessionKey
			}).then((res)=>{
				if (res.code == 2000) {
					wx.setStorageSync("cellphone",res.data.phoneNumber);
					this.login(res.data.phoneNumber);
				}
			});
			*/
		}
	},
	// 页面显示事件
	onShow: function () {
		// 已登录,自动跳转回主页
    	if (wx.getStorageSync("strUserId")) {
        	wx.redirectTo({ url:"/pages/main/main" });
		}
	}
})